<?php
namespace backend\controller;
use backend\model\admin_model;
use backend\model\admin_role;
use backend\model\role_auth;

class BackendBaseController{
    protected static $admin = null;
    protected  $view  = null;

    /**
    从session获取管理员信息
    */
    public function getSessionAdmin()
    {
        return !empty($_SESSION['admin']) ? $_SESSION['admin'] : false;
    }
    
    public function __construct()
    {
        session_start();

        $session_admin = $this->getSessionAdmin();
        if(empty($session_admin) && in_array(C_ACTION,['login'])){
            return;
        }
        if(empty($session_admin) && !in_array(C_ACTION,['login'])){
            redirect("admin/login");
        }else{
            $check = $this->checkAuth();
            if(!$check){
                ajaxReturn(['msg'=>'当前的操作没有权限','code'=>-10000]);
            }
        }
        $auth_model = new role_auth();
        $defaultMenu = $auth_model->getRoleAuthMenu($session_admin['role_id'],$_GET['s']);
        $this->assign('defaultMenu',$defaultMenu);

    }

    /**
     * 权限验证
     * @return bool
     */
    private function checkAuth()
    {
        $session_admin = $this->getSessionAdmin();
        if(empty($session_admin)){
            return false;
        }
        //超级管理员默认都有权限
        if($session_admin['role_id'] == admin_role::ID_SUPERADMIN){
            return true;
        }

        $admin_model = new admin_model();
        //当前管理员权限列表
        if(empty($session_admin['auth_list']) && isset($session_admin['role_id']) ){
            $auth_list = $admin_model->getRoleAuthList($session_admin['role_id']);
            $_SESSION['admin']['auth_list'] = $auth_list;
        }
        $session_admin = $this->getSessionAdmin();
        if(empty($session_admin['auth_list'])){
            return false;
        }
        $auth_result = $admin_model->checkAuth($session_admin['auth_list'],$_GET['s']);
        return $auth_result;
    }

    protected function viewInit()
    {
        require FRAMEWORK_DIR.'/../libs/smarty/libs/Smarty.class.php';
        $smarty = new \Smarty;

        $base_dir = APPLICATION_DIR.C_MODULE.DIRECTORY_SEPARATOR.'view'.DIRECTORY_SEPARATOR;
        $smarty->template_dir = $base_dir.ucfirst(C_CONTROLLER).DIRECTORY_SEPARATOR;
        $smarty->compile_dir = APPLICATION_DIR.DIRECTORY_SEPARATOR.C_MODULE.DIRECTORY_SEPARATOR.'compile/';//编译目录
        //$smarty->force_compile = true;
        $smarty->debugging = false;
        $smarty->caching = false;
        $smarty->left_delimiter = '<{';
        $smarty->right_delimiter = '}>';
        return $smarty;
    }

    /**
     * 向模板赋值
     * @param $key
     * @param $value
     */
    protected function assign($key,$value='')
    {
        if(is_null($this->view)){
            $this->view = $this->viewInit();
        }
        if(is_array($key)){
            foreach($key AS $k=>&$v){
                $this->view->assign($k,$v);
            }
        }else{
            $this->view->assign($key,$value);
        }
    }

    /**
     * 显示模板
     * @param $tpl
     */
    protected function display($tplName='')
    {
        if(is_null($this->view)){
            $this->view = $this->viewInit();
        }
        if(empty($tplName)) $tplName = C_ACTION;
        if(stripos($tplName,'.html') === false){
            $tplName .= '.html';
        }
        $this->view->display($tplName);
    }
}